// 封装网络请求对象
// 基地址
import store from '../store'
let BASE_URL = 'https://api-hmugo-web.itheima.net/api/public/v1/'
function request ({
  url,
  method = 'GET',
  data = {},
  header = {},
  tip = '正在加载中。。。',
  isPullDown = false
}) {
  return new Promise((resolve, reject) => {
    // 如果是正常的请求数据则正常显示loading效果，如果是下拉数据则不显示loading效果
    if (!isPullDown) {
      // 开启菊花
      uni.showLoading({
        title: tip,
      })
    }
    // 判断是否有token
    console.log(store.getters['user/getToken']);
    if (store.getters['user/getToken']) {
      header.Authorization = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjU0LCJpYXQiOjE2NDI4MjA0NjcsImV4cCI6MTAwMTY0MjgyMDQ2Nn0.euo_T0z46njSNZdElnVx-4IDPhl4oibXMR2gaxoHQwY'
    }
    uni.request({
      url: `${BASE_URL}${url}`,
      method,
      data,
      header,
      success: (res) => {
        resolve(res.data)
      },
      fail: err => {
        reject(err)
      },
      complete: () => {
        // 不管请求成功还是失败都要关闭showloading
        uni.hideLoading()
      }
    })
  })
}
uni.$request = request
export {
  request
}